package com.certificationManager.client.view;

import com.certificationManager.client.i18n.AppConstants;
import com.certificationManager.client.presenter.view_interface.IRegistrationView;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class RegistrationView extends Composite implements IRegistrationView {

	/** Internationalization */
	AppConstants i18n = GWT.create(AppConstants.class);
	
	/** Fields */
	private final VerticalPanel inscriptionPanel = new VerticalPanel();
	
	private final FormPanel inscriptionFormPanel = new FormPanel();

	private final FlexTable inscriptionLayout = new FlexTable();

	private final TextField<String> firstnameField = new TextField<String>();
	private final TextField<String> lastnameField = new TextField<String>();
	private final TextField<String> emailField = new TextField<String>();
	private final TextField<String> passField = new TextField<String>();
	private final TextField<String> passBisField = new TextField<String>();
	private final Button registrationButton = new Button(i18n.inscription_RegistrationButton());
	private final Button backButton = new Button(i18n.inscription_BackButton());
	private final HTML title = new HTML();
	private final HTML subTitle = new HTML();
	
	public RegistrationView() {
	
		inscriptionPanel.setWidth("94%");
		inscriptionPanel.setSpacing(20);
		inscriptionPanel.add(title);
		inscriptionPanel.add(subTitle);
		inscriptionPanel.add(inscriptionFormPanel);

		passField.setPassword(true);
		passBisField.setPassword(true);
		
		firstnameField.setAllowBlank(false);
		lastnameField.setAllowBlank(false);
		emailField.setAllowBlank(false);
		passField.setAllowBlank(false);
		passBisField.setAllowBlank(false);
		
		emailField.setRegex(".+@.+\\.[a-z]+");
		emailField.getMessages().setRegexText("Veuillez saisir une adresse email valide.");
		
		firstnameField.getMessages().setBlankText("Ce champ n'est pas optionnel.");
		lastnameField.getMessages().setBlankText("Ce champ n'est pas optionnel.");
		emailField.getMessages().setBlankText("Ce champ n'est pas optionnel.");
		passField.getMessages().setBlankText("Ce champ n'est pas optionnel.");
		passBisField.getMessages().setBlankText("Ce champ n'est pas optionnel.");
		
		
		// Create a table to loginLayout the form options
		inscriptionLayout.setCellSpacing(6);
		
		inscriptionLayout.setHTML(0, 0, i18n.nom());
	    inscriptionLayout.setWidget(0, 1, firstnameField);
	    inscriptionLayout.setHTML(1, 0, i18n.prenom());
	    inscriptionLayout.setWidget(1, 1, lastnameField);
	    inscriptionLayout.setHTML(2, 0, i18n.email());
	    inscriptionLayout.setWidget(2, 1, emailField);
	    inscriptionLayout.setHTML(3, 0, i18n.pass());
	    inscriptionLayout.setWidget(3, 1, passField);
	    inscriptionLayout.setHTML(4, 0, "Password Bis : ");
	    inscriptionLayout.setWidget(4, 1, passBisField);
	    inscriptionLayout.setWidget(5, 0, backButton);
	    inscriptionLayout.setWidget(5, 1, registrationButton);

	    title.setHTML("<h1 style=\"text-align:left;\">Créez un compte</h1>");
	    subTitle.setHTML("Votre compte Certification-Manager vous permet d'accéder aux différents tests relatifs aux certifications.<br />Si vous possédez déjà un compte Certification-Manager, vous pouvez <a href=\"#\">cliquer sur ce lien pour vous connecter</a>.");
	    
		inscriptionFormPanel.add(inscriptionLayout);
		inscriptionFormPanel.setHeading(i18n.inscription_Title());
		inscriptionFormPanel.setWidth("300px");
		
		initWidget(inscriptionPanel);
		
	}

	@Override
	public Field<String> getFirstnameField() {
		return firstnameField;
	}

	@Override
	public Field<String> getLastnameField() {
		return lastnameField;
	}
	
	@Override
	public Field<String> getEmailField() {
		return emailField;
	}
	
	@Override
	public Field<String> getPassField() {
		return passField;
	}
	
	@Override
	public Field<String> getPassBisField() {
		return passBisField;
	}

	@Override
	public Button getRegistrationButton() {
		return registrationButton;
	}
	
	@Override
	public Button getBackButton() {
		return backButton;
	}

	@Override
	public Widget getViewWidget() {
		return this;
	}
	
}
